package im_server

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"pim/pkg/promePkg"
)

func (s *server) InitHttpApi() {
	rootMod := s.httpd

	apiMod := rootMod.Group("api")

	_ = apiMod

	rootMod.Use(func(ctx *gin.Context) {
		method := ctx.Request.Method

		ctx.Header("Access-Control-Allow-Origin", "*")
		ctx.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
		ctx.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
		ctx.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		ctx.Header("Access-Control-Allow-Credentials", "true")

		//ctx.AbortWithStatus(http.StatusNoContent)
		if method == "OPTIONS" {
			//ctx.JSON(http.StatusOK, "Options Request!")
			ctx.AbortWithStatus(http.StatusNoContent)

		}

		ctx.Next()
	})

	apiMod.GET("metrics", promePkg.PrometheusHandler())
	apiMod.GET("gateway", s.HttpGetWsGateway)
	apiMod.GET("web_gateway", s.HttpGetWsWebGateway)
	apiMod.GET("pio", s.HttpGetFileIO)

	authMod := apiMod.Group("auth")

	authMod.POST("register", s.HttpPostRegisterUser)
	authMod.POST("login", s.HttpPostLoginToken)

}
